//
//  ActivityTableViewCell.m
//  豆瓣
//
//  Created by aaaaaa on 16/6/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ActivityTableViewCell.h"
#import "ActivityModel.h"
#import "UIImageView+WebCache.h"
#import "Tools.h"

#define IMAGEPATH(IMAGENAME, TYPE) [UIImage imageWithData:[NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:(IMAGENAME) ofType:(TYPE)]]]
#define CONTENTVIEWHEIGHT self.contentView.bounds.size.height
#define CONTENTVIEWWIDTH self.contentView.bounds.size.width

#define LOGSIZE 25
#define SPACING 5



@interface ActivityTableViewCell ()

@property (nonatomic, retain) UIImageView *blueImageView;    //蓝背景
@property (nonatomic, retain) UILabel *titleLable;          //标题Lable
@property (nonatomic, retain) UIImageView *whiteImageView;  //白背景
@property (nonatomic, retain) UIImageView *catalogImageView;    //类型图标
@property (nonatomic, retain) UILabel *catalogLable;       //类型Lable
@property (nonatomic, retain) UIImageView *dateImageView;  //时间图标
@property (nonatomic, retain) UILabel *dateLable;          //时间lalbe
@property (nonatomic, retain) UIImageView *spotImageView;  //地点图标
@property (nonatomic, retain) UILabel *spotLable;          //地点Lable
@property (nonatomic, retain) UILabel *interestedLable;    //感兴趣
@property (nonatomic, retain) UILabel *participateLable;   //参加
@property (nonatomic, retain) UIImageView *contentImageView;      //图片



@end

@implementation ActivityTableViewCell

- (void)dealloc
{
    [_blueImageView release];
    [_titleLable release];
    [_whiteImageView release];
    [_catalogImageView release];
    [_catalogLable release];
    [_dateImageView release];
    [_dateLable release];
    [_spotImageView release];
    [_spotLable release];
    [_interestedLable release];
    [_participateLable release];
    [_contentImageView release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createSubViews];
    }
    return self;
}

- (void)createSubViews{
    
    self.blueImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:_blueImageView];
    [_blueImageView release];
    
    self.titleLable = [[UILabel alloc] init];
    [self.blueImageView addSubview:_titleLable];
    [_titleLable release];
    
    self.whiteImageView = [[UIImageView alloc] init];
    [self.blueImageView addSubview:_whiteImageView];
    [_whiteImageView release];
    
    
    self.catalogImageView = [[UIImageView alloc] init];
    [self.whiteImageView addSubview:_catalogImageView];
    [_catalogImageView release];
    
    self.catalogLable = [[UILabel alloc] init];
    [self.whiteImageView addSubview:_catalogLable];
    [_catalogLable release];
    
    
    self.dateImageView = [[UIImageView alloc] init];
    [self.whiteImageView addSubview:_dateImageView];
    [_dateImageView release];
    
    self.dateLable = [[UILabel alloc] init];
    [self.whiteImageView addSubview:_dateLable];
    [_dateLable release];
    
    
    self.spotImageView = [[UIImageView alloc] init];
    [self.whiteImageView addSubview:_spotImageView];
    [_spotImageView release];
    
    self.spotLable = [[UILabel alloc] init];
    [self.whiteImageView addSubview:_spotLable];
    [_spotLable release];
    
    
    self.interestedLable = [[UILabel alloc] init];
    [self.whiteImageView addSubview:_interestedLable];
    [_interestedLable release];
    
    self.participateLable = [[UILabel alloc] init];
    [self.whiteImageView addSubview:_participateLable];
    [_participateLable release];
    
    self.contentImageView = [[UIImageView alloc] init];
    [self.whiteImageView addSubview:_contentImageView];
    [_contentImageView release];
}



- (void)layoutSubviews{
    [super layoutSubviews];
    
    //蓝背景
    self.blueImageView.image = IMAGEPATH(@"bg_eventlistcell", @"png");
    self.blueImageView.frame = CGRectMake(10, 10,CONTENTVIEWWIDTH - 20 , CONTENTVIEWHEIGHT - 20);
    
    //标题Lable
    self.titleLable.frame = CGRectMake(15, 3, self.blueImageView.bounds.size.width - 25, 40);
    self.titleLable.font = [UIFont systemFontOfSize:18];
    
    //白背景
    self.whiteImageView.image = IMAGEPATH(@"bg_share_large", @"png");
    self.whiteImageView.frame = CGRectMake(2, 40, self.blueImageView.bounds.size.width - 4, self.blueImageView.bounds.size.height - 35);
    
    //时间图标
    self.dateImageView.image = IMAGEPATH(@"icon_date", @"png");
    self.dateImageView.frame = CGRectMake(10, SPACING, LOGSIZE, LOGSIZE);
    
    //时间Lable
    self.dateLable.frame = CGRectMake(10 + LOGSIZE, SPACING, self.whiteImageView.bounds.size.width * 0.65, LOGSIZE);
//    self.dateLable.backgroundColor = [UIColor orangeColor];
    self.dateLable.font = FONT;
    
    //地点图标
    self.spotImageView.image = IMAGEPATH(@"icon_spot", @"png");
    self.spotImageView.frame = CGRectMake(10, SPACING * 2 + LOGSIZE, LOGSIZE, LOGSIZE);
    
    //地点Lable
    self.spotLable.frame = CGRectMake(10 + LOGSIZE, SPACING * 2 + LOGSIZE, self.whiteImageView.bounds.size.width * 0.65, LOGSIZE);
//    self.spotLable.backgroundColor = [UIColor redColor];
    self.spotLable.font = FONT;
    
    //类型图标
    self.catalogImageView.image = IMAGEPATH(@"icon_catalog", @"png");
    self.catalogImageView.frame = CGRectMake(10, SPACING * 3 + LOGSIZE * 2, LOGSIZE, LOGSIZE);
    
    //类型Lable
    self.catalogLable.frame = CGRectMake(10 + LOGSIZE, SPACING * 3 + LOGSIZE * 2, self.whiteImageView.bounds.size.width * 0.6, LOGSIZE);
//    self.catalogLable.backgroundColor = [UIColor blackColor];
    self.catalogLable.font = FONT;
    
    //感兴趣Lable
    self.interestedLable.frame = CGRectMake(20, self.whiteImageView.bounds.size.height * 0.75, self.whiteImageView.bounds.size.width * 0.3, LOGSIZE);
//    self.interestedLable.backgroundColor = [UIColor purpleColor];
//    self.interestedLable.text = @"感兴趣:";
    self.interestedLable.font = FONT;
    
    //参加Lable
    self.participateLable.frame = CGRectMake(self.whiteImageView.bounds.size.width * 0.3 + 20, self.whiteImageView.bounds.size.height * 0.75, self.whiteImageView.bounds.size.width * 0.3, LOGSIZE);
//    self.participateLable.backgroundColor = [UIColor grayColor];
    self.participateLable.font = FONT;
    
    //图片
    CGFloat height = [Tools getImageHeight:self.activityModel.image withWidth:self.whiteImageView.bounds.size.width * 0.25 - 5];
    self.contentImageView.frame = CGRectMake(self.whiteImageView.bounds.size.width * 0.75, 3, self.whiteImageView.bounds.size.width * 0.25 - 5, height);
//    self.contentImageView.backgroundColor = [UIColor yellowColor];
    
}

- (void)setActivityModel:(ActivityModel *)activityModel{
    if (_activityModel != activityModel) {
        [_activityModel release];
        _activityModel = [activityModel retain];
    }
    
    self.titleLable.text = activityModel.title;
    self.spotLable.text = activityModel.address;
    self.catalogLable.text = [NSString stringWithFormat:@"类型：%@", activityModel.category_name] ;
    
    
    //时间

    NSDate *startDate = [Tools returnDate:activityModel.begin_time];
    NSDate *endDate = [Tools returnDate:activityModel.end_time];
    
    NSString *startStr = [Tools returnString:startDate];
    NSString *endStr = [Tools returnString:endDate];

    
    self.dateLable.text = [NSString stringWithFormat:@"%@ -- %@", startStr, endStr];
    
    
    
    
    
    
    //感兴趣
    NSMutableAttributedString *interstedStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"感兴趣：%ld", activityModel.wisher_count]];
    
    NSRange insterstedRange = NSMakeRange([[interstedStr string] rangeOfString:[NSString stringWithFormat:@"感兴趣："]].length, [[interstedStr string] rangeOfString:[NSString stringWithFormat:@"%ld", activityModel.wisher_count]].length);
    [interstedStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:insterstedRange];
    [self.interestedLable setAttributedText:interstedStr];
    
    //参加
    NSMutableAttributedString *participateStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"参加：%ld", activityModel.participant_count]];
    
    NSRange participateRange = NSMakeRange([[participateStr string] rangeOfString:[NSString stringWithFormat:@"参加："]].length, [[participateStr string] rangeOfString:[NSString stringWithFormat:@"%ld", activityModel.participant_count]].length);
    
    [participateStr addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:participateRange];
    
    [self.participateLable setAttributedText:participateStr];
    
    
    //图片
    [self.contentImageView sd_setImageWithURL:[NSURL URLWithString:activityModel.image]];
    
    
    
}










@end
